package de.tu_darmstadt.informatik.tk.lernsystem.util;

import java.util.List;

import de.tu_darmstadt.informatik.tk.lernsystem.concepts.Rating;
import de.tu_darmstadt.informatik.tk.lernsystem.concepts.Section;
import de.tu_darmstadt.informatik.tk.lernsystem.concepts.Tag;
import de.tu_darmstadt.informatik.tk.lernsystem.concepts.Thing;
import de.tu_darmstadt.informatik.tk.lernsystem.concepts.User;

/** 
 * Provides helper methods easing the work
 * with various concepts.
 */
public class ConceptHelper {
	/**
	 * Returns a user's resource rating.
	 * @param user
	 * @param section
	 * @return Rating, or -1 if user has not rated the resource
	 */
	public static Integer getMyRating(User user, Thing section) {
		//System.out.println(section.getRatings());
		if (section.getRatings() == null) {
			return -1;
		}
		for (Rating r : section.getRatings()) {
			if (user.equals(r.getOwner())) {
				return r.getRating();
			}
		}
		return -1;
	}
	
	/**
	 * Calculates a resource's average rating.
	 * @param user
	 * @param section
	 * @return Avarage rating, or -1 if noone has rated this resource yet.
	 */
	public static Double getAverageRating(User user, Thing section) {
		if (section.getRatings() == null || section.getRatings().isEmpty()) {
			return -1d;
		}
		Integer total = 0;
		for (Rating r : section.getRatings()) {
			total += r.getRating();
		}
		return (double)total / (double)section.getRatings().size();
	}
	
	/**
	 * Creates an imploded list from a list of tags.
	 * @param tags
	 * @return
	 */
	public static String implodeTagList(List<Tag> tags) {
		String s = "";
		for (Tag t : tags) {
			if (!s.isEmpty()) {
				s += ", ";
			}
			s += t.getTag();
		}
		return s;
	}
}
